(ns liberator-sample.core.tutorial.content-negotiation
  (:require [liberator.core :refer [defresource resource]]
            [liberator.dev :refer [wrap-trace]]
            [compojure.core :refer [defroutes ANY]]
            [ring.middleware.params :refer [wrap-params]]
            [org.httpkit.server :as server]))

(declare app)
(defroutes app
           (ANY "/babel" []
                (resource :available-media-types ["text/plain" "text/html"
                                                  "application/json" "application/clojure;q=0.9"]
                          :handle-ok
                          #(let [media-type (get-in % [:representation :media-type])]
                            (condp = media-type
                              "text/plain" "You requested plain text."
                              "text/html" "<html><h1>You requested HTML</h1></html>"
                              {:message    "You requested a media type"
                               :media-type media-type}))
                          :handle-not-acceptable "Un, Oh, I cannot speak those languages!")))

(server/run-server (wrap-trace app :ui) {:port 3333})